import {EventEmitter} from 'events';
import AppDispatcher from './AppDispatcher'
import * as ActionTypes from './ActionTypes.js'

export const counterValues = {'First':0,'Second':111,'Third':333};
export const CHANGE_EVENT = "changeEvent";

const CounterStore = Object.assign({},EventEmitter.prototype,{

    getCounterValues: function() {
        return counterValues;
    },

    emitChange: function(){
        this.emit(CHANGE_EVENT);
    },
    addChangeListener: function(callback){
        this.on(CHANGE_EVENT,callback);
    },
    removeChangeListener: function(callback){
        this.removeListener(CHANGE_EVENT,callback);
    }
});

CounterStore.dispatchToken = AppDispatcher.register((action)=>{
    console.log("********** type::" + action.type);
    if(action.type == ActionTypes.TYPE_INCREMENT){
        counterValues[action.counterCaption]++;
        CounterStore.emitChange();
        console.log("##################execute increment");
    }else if(action.type == ActionTypes.TYPE_DECREMENT){
        counterValues[action.counterCaption]--;
        CounterStore.emitChange();
        console.log("##################execute decrement");
    }
});

export default CounterStore;